package com.time.workbench.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.time.workbench.core.util.AssertUtil;
import com.time.workbench.system.entity.bo.SystemDictType;
import com.time.workbench.system.entity.vo.SystemDictTypeVO;
import com.time.workbench.system.entity.vo.SystemDictVO;
import com.time.workbench.system.mapper.SystemDictTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictTypeService extends ServiceImpl<SystemDictTypeMapper, SystemDictType> {

    @Autowired
    private SystemDictService systemDictService;

    public SystemDictTypeVO getByTypeCode(String typeCode) {
        // 获取字典类型数据
        SystemDictType systemDictType = lambdaQuery().eq(SystemDictType::getCode, typeCode).one();
        AssertUtil.notNull(systemDictType, "未配置字典:" + typeCode);
        // 获取所有字典
        List<SystemDictVO> systemDictVOS = systemDictService.getByTypeCode(typeCode);

        // 组装数据
        SystemDictTypeVO systemDictTypeVO = new SystemDictTypeVO();
        BeanUtils.copyProperties(systemDictType, systemDictTypeVO);
        systemDictTypeVO.setDict(systemDictVOS);

        return systemDictTypeVO;
    }
}
